<?php
/**
 * Validate Base Class
 * User: zng
 * Date: 2019/9/11
 * Time: 09:11
 */
namespace app\admin\validate;

class Admin extends Validates
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'ids|标识' => 'require|array',
        'id|标识' => 'integer',
        'username|账户' => 'require|length:4,18|alphaNum',
        'password|密码' => 'require|length:6,18|alphaDash',
        'password_confirm|确认密码' => 'confirm:password',
        'captcha|验证码' => 'require|captcha',
        'online|保持登录状态' => 'eq:1',
        'mobile|手机号码' => 'mobile',
        'status|状态' => 'in:0,1',
        'start_create_at|开始创建时间范围' => 'date',
        'end_create_at|结束创建时间范围' => 'date',
        'rid|角色标识' => 'require|array',
        'skin|皮肤' => 'require|alphaDash'
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */
    protected $message = [];

    /**
     * 场景验证
     * @var []
     */
    protected $scene = [
        'login' => ['username','password','captcha','online']
    ];

    /**
     * 定义登录场景验证
     * @return $this
     */
    public function sceneIndex()
    {
        return $this->only(['username','status','mobile','start_create_at','end_create_at'])
            ->remove('username', 'require')
            ->append('pageSize', 'integer');
    }

    /**
     * 定义编辑场景验证
     * @return $this
     */
    public function sceneUpdate()
    {
        return $this->only(['password','password_confirm','mobile','rid'])
            ->remove('password', 'require')
            ->remove('rid', 'require');
    }

    /**
     * 定义新增场景验证
     * @return $this
     */
    public function sceneCreate()
    {
        return $this->only(['username','password','password_confirm','mobile','rid'])
            ->append('username', 'unique:admin')
            ->append('password_confirm', 'require');
    }

    /**
     * 定义验证用户名唯一性场景验证
     * @return $this
     */
    public function sceneOnly()
    {
        return $this->only(['id','username']);
    }

    /**
     * 定义批量删除场景验证
     * @return $this
     */
    public function sceneDestory()
    {
        return $this->only(['ids']);
    }

    /**
     * 定义管理员换肤场景
     * @return $this
     */
    public function sceneSkin()
    {
        $config = config('field.admin.skin');
        $skin = [];
        foreach ( $config as $_config ) {
            $skin[] = $_config['value'];
        }
        return $this->only(['skin'])
            ->append('skin', 'in:'.implode(',', $skin));
    }

}
